package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Message;
import com.zhitong.money.services.api.IMessageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: Andy Zhang
 * @Description:
 * @Date: 17:14 2021/2/25
 * @Company: wisecard
 */
@CrossOrigin
@RestController
public class MessageContro {

    @Autowired
    private IMessageService messageService;

    @GetMapping("/msgs")
    public AjaxResponse getAllMsg() throws Exception{
        List<Message> allMsg = messageService.findAllMsg();
        return AjaxResponse.success(allMsg);
    }

    @GetMapping("/msg/{id}")
    public AjaxResponse getMsgById(@PathVariable String id) throws  Exception{
        Message msgById = messageService.findMsgById(id);
        return AjaxResponse.success(msgById);
    }

    @GetMapping("/selfMsg/{tid}")
    public AjaxResponse getSelfMsg(@PathVariable String tid) throws Exception{
        List<Message> selfMsg = messageService.findSelfMsg(tid);
        return AjaxResponse.success(selfMsg);
    }


    @PostMapping("/msgs")
    public AjaxResponse getMsgByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo<Message> msgByPage = messageService.findMsgByPage(map);
        return AjaxResponse.success(msgByPage);
    }

    @RequiresPermissions({"addMessage"})
    @PostMapping("/msg")
    public AjaxResponse addMsg(@RequestBody Map<String,Object> map)throws Exception{
        messageService.addMsg(map);
        return AjaxResponse.success("添加成功!");
    }

    @RequiresPermissions({"updateMessage"})
    @PutMapping("/msg")
    public AjaxResponse updateMsg(@RequestBody Message message) throws Exception{
        messageService.updateMsg(message);
        return AjaxResponse.success("修改成功!");
    }

    @RequiresPermissions({"delMessage"})
    @DeleteMapping("/msg/{id}")
    public AjaxResponse delMsgById(@PathVariable String id)throws Exception{
        messageService.delMsgById(id);
        return AjaxResponse.success("删除成功!");
    }

    @DeleteMapping("/selfMsg/{mid}")
    public AjaxResponse delSelfMsg(@PathVariable String mid) throws Exception{
        messageService.delBySelf(mid);
        return AjaxResponse.success("删除成功");
    }

    @RequiresPermissions({"delMessage"})
    @PostMapping("/delmsgs")
    public AjaxResponse delMsgByIds(@RequestBody List<String> ids) throws Exception{
        messageService.delMsgByIds(ids);
        return AjaxResponse.success("删除成功!");
    }

    @PostMapping("/images")
    public AjaxResponse uploadImage(@RequestBody MultipartFile file) throws Exception{
        if (file.isEmpty()){
            return null;
        }
        String originName=file.getOriginalFilename();
        String suffixName = originName.substring(originName.lastIndexOf("."));  // 后缀名
        String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/images"; // 上传后的路径
        String fileName= UUID.randomUUID().toString().replaceAll("-","")+suffixName;
        File file1=new File(filePath,fileName);
        if (!file1.getParentFile().exists()){
            file1.getParentFile().mkdirs();
        }

        file.transferTo(file1);
        String url="http://127.0.0.1:8090/images/"+fileName;
        return AjaxResponse.success(url);


    }
}
